<template>
  <div class="realtime-logs">
    <div class="logs-header">
      <h4>报警</h4>
      <div class="logs-controls">
        <div class="logs-stats">
          <span class="stats-item">
            <span class="stats-label">总数:</span>
            <span class="stats-value">{{ logCount }}</span>
          </span>
        </div>
        <div class="logs-actions">
          <button class="action-btn clear-logs-btn" @click="clearAllLogs" title="清除所有日志">
            <svg viewBox="0 0 24 24" fill="currentColor">
              <path d="M19,4H15.5L14.5,3H9.5L8.5,4H5V6H19M6,19A2,2 0 0,0 8,21H16A2,2 0 0,0 18,19V7H6V19Z" />
            </svg>
          </button>
        </div>
      </div>
    </div>
    <div class="logs-container">
      <!-- 空状态 -->
      <div v-if="!latestLogs || latestLogs.length === 0" class="logs-empty-state">
        <div class="empty-icon">
          <svg viewBox="0 0 24 24" fill="currentColor">
            <path d="M19,7H18V6A2,2 0 0,0 16,4H8A2,2 0 0,0 6,6V7H5A3,3 0 0,0 2,10V19A3,3 0 0,0 5,22H19A3,3 0 0,0 22,19V10A3,3 0 0,0 19,7M16,6V7H8V6H16M19,19H5V10H19V19Z" />
          </svg>
        </div>
        <div class="empty-text">
          <h4>暂无警告</h4>
          <p>暂无实时警告数据</p>
        </div>
      </div>

      <!-- 日志列表 -->
      <div v-else class="logs-list" ref="logsList">
        <div
          v-for="log in latestLogs"
          :key="log.id"
          :class="['log-item', `log-${log.type}`]"
        >
          <div class="log-icon">
            <svg v-if="log.type === 'info'" viewBox="0 0 24 24" fill="currentColor">
              <path d="M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2M13,17H11V15H13V17M13,13H11V7H13V13Z" />
            </svg>
            <svg v-else-if="log.type === 'success'" viewBox="0 0 24 24" fill="currentColor">
              <path d="M12,2A10,10 0 0,1 22,12A10,10 0 0,1 12,22A10,10 0 0,1 2,12A10,10 0 0,1 12,2M11,16.5L18,9.5L16.59,8.09L11,13.67L7.91,10.59L6.5,12L11,16.5Z" />
            </svg>
            <svg v-else-if="log.type === 'warning'" viewBox="0 0 24 24" fill="currentColor">
              <path d="M12,2L3.09,21H20.91L12,2M13,18H11V16H13V18M13,14H11V10H13V14Z" />
            </svg>
            <svg v-else-if="log.type === 'error'" viewBox="0 0 24 24" fill="currentColor">
              <path d="M12,2A10,10 0 0,1 22,12A10,10 0 0,1 12,22A10,10 0 0,1 2,12A10,10 0 0,1 12,2M13,17H11V15H13V17M13,13H11V7H13V13Z" />
            </svg>
          </div>
          <div class="log-content">
            <div class="log-message">{{ log.message }}</div>
            <div class="log-time">{{ formatLogTime(log.timestamp) }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'RealtimeLogs',
  data() {
    return {
      simulationTimer: null
    }
  },
  computed: {
    // 获取日志数据
    logs() {
      return this.$store.getters['logs/getLogs']
    },

    // 获取最新的日志（显示前20条）
    latestLogs() {
      return this.$store.getters['logs/getLatestLogs'](20)
    },

    // 获取日志总数
    logCount() {
      return this.$store.getters['logs/getLogCount']
    },
  },

  mounted() {
    // 组件加载完成后，如果有日志则滚动到底部
    this.$nextTick(() => {
      if (this.latestLogs && this.latestLogs.length > 0) {
        setTimeout(() => {
          this.scrollToLatestLog()
        }, 100)
      }
    })

    // 启动模拟报警信息定时器
    this.startSimulation()
  },

  beforeDestroy() {
    // 清理定时器
    this.stopSimulation()
  },

  watch: {
    // 监听日志变化，自动滚动到最新
    logs: {
      handler() {
        this.scrollToLatestLog()
      },
      deep: true
    }
  },

  methods: {
    // 自动滚动到最新日志
    scrollToLatestLog() {
      // 使用多次 $nextTick 和 setTimeout 来确保 DOM 完全更新
      this.$nextTick(() => {
        const logsList = this.$refs.logsList
        if (logsList) {
          // 使用 requestAnimationFrame 确保渲染完成
          requestAnimationFrame(() => {
            // 再次延迟，确保内容完全渲染
            setTimeout(() => {
              logsList.scrollTop = 0
            }, 50)
          })
        }
      })
    },

    // 格式化日志时间
    formatLogTime(timestamp) {
      const date = new Date(timestamp)
      const now = new Date()
      const diff = now - date

      // 小于1分钟
      if (diff < 60000) {
        return '刚刚'
      }
      // 小于1小时
      else if (diff < 3600000) {
        const minutes = Math.floor(diff / 60000)
        return `${minutes}分钟前`
      }
      // 小于24小时
      else if (diff < 86400000) {
        const hours = Math.floor(diff / 3600000)
        return `${hours}小时前`
      }
      // 超过24小时
      else {
        return date.toLocaleString('zh-CN', {
          month: 'short',
          day: 'numeric',
          hour: '2-digit',
          minute: '2-digit'
        })
      }
    },

    // 清除所有日志
    clearAllLogs() {
      if (confirm('确定要清除所有日志吗？此操作不可恢复。')) {
        this.$store.dispatch('logs/clearAllLogs')
      }
    },

    // 启动模拟报警信息
    startSimulation() {
      // 每8-15秒随机生成一条报警信息
      const generateRandomInterval = () => Math.random() * 7000 + 8000 // 8000-15000ms

      const generateLog = () => {
        this.generateRandomAlarm()
        // 设置下一次生成的时间
        this.simulationTimer = setTimeout(generateLog, generateRandomInterval())
      }

      // 立即开始第一次生成
      generateLog()
    },

    // 停止模拟报警信息
    stopSimulation() {
      if (this.simulationTimer) {
        clearTimeout(this.simulationTimer)
        this.simulationTimer = null
      }
    },

    // 生成固定报警信息
    generateRandomAlarm() {
      // 固定日志数组
      const fixedLogs = [
        { type: 'warning', message: '白湖分局串河监区第一分监区承载能力告警' },
        { type: 'error', message: '潜川监狱第五监区有关联罪犯' },
        { type: 'warning', message: '九城分局马家湖监区第三分监区危险犯较多' }
      ]

      // 随机选择一个固定日志
      const randomLog = fixedLogs[Math.floor(Math.random() * fixedLogs.length)]

      // 添加日志到store
      this.$store.dispatch('logs/addLog', {
        type: randomLog.type,
        message: randomLog.message,
        timestamp: Date.now()
      })
    }
  }
}
</script>

<style scoped>
.realtime-logs {
  overflow: hidden;
  height: 100%;
  background:
    linear-gradient(135deg,
      rgba(0, 255, 255, 0.08) 0%,
      rgba(255, 0, 255, 0.06) 50%,
      rgba(255, 255, 0, 0.04) 100%
    ),
    rgba(0, 0, 0, 0.4);
  border-radius: 12px;
  border: 1px solid rgba(0, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  animation: logsSlideIn 0.5s ease-out;
}

@keyframes logsSlideIn {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.logs-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
  background: rgba(0, 255, 255, 0.05);
  border-bottom: 1px solid rgba(0, 255, 255, 0.1);
}

.logs-header h4 {
  margin: 0;
  color: #00ffff;
  font-size: 15px;
  font-weight: 600;
  text-shadow: 0 0 10px rgba(0, 255, 255, 0.5);
}

.logs-controls {
  display: flex;
  align-items: center;
  gap: 15px;
}

.logs-stats {
  display: flex;
  gap: 15px;
}

.logs-actions {
  display: flex;
  gap: 8px;
}

.clear-logs-btn {
  width: 28px;
  height: 28px;
  padding: 0;
  background:
    linear-gradient(135deg,
      rgba(255, 68, 68, 0.2) 0%,
      rgba(255, 68, 68, 0.1) 100%
    ),
    rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(255, 68, 68, 0.4);
  border-radius: 6px;
  color: #ff4444;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.clear-logs-btn:hover {
  background:
    linear-gradient(135deg,
      rgba(255, 68, 68, 0.3) 0%,
      rgba(255, 68, 68, 0.2) 100%
    ),
    rgba(0, 0, 0, 0.6);
  border-color: rgba(255, 68, 68, 0.6);
  box-shadow: 0 0 15px rgba(255, 68, 68, 0.3);
  transform: scale(1.05);
}

.clear-logs-btn svg {
  width: 16px;
  height: 16px;
}

.stats-item {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
}

.stats-label {
  color: rgba(255, 255, 255, 0.7);
}

.stats-value {
  color: #00ffff;
  font-weight: 600;
  text-shadow: 0 0 5px rgba(0, 255, 255, 0.5);
}

.logs-container {
  flex: 1;
  overflow: hidden;
  position: relative;
}

.logs-empty-state {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  color: rgba(255, 255, 255, 0.7);
  animation: emptyStateFadeIn 0.5s ease-out;
}

@keyframes emptyStateFadeIn {
  from {
    opacity: 0;
    transform: translate(-50%, -60%);
  }
  to {
    opacity: 1;
    transform: translate(-50%, -50%);
  }
}

.empty-icon {
  width: 64px;
  height: 64px;
  margin: 0 auto 20px auto;
  color: rgba(0, 255, 255, 0.4);
  background:
    radial-gradient(circle, rgba(0, 255, 255, 0.1) 0%, rgba(0, 255, 255, 0.05) 50%, transparent 70%),
    rgba(0, 0, 0, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid rgba(0, 255, 255, 0.2);
  box-shadow: 0 0 20px rgba(0, 255, 255, 0.1);
  animation: emptyIconPulse 3s ease-in-out infinite;
}

@keyframes emptyIconPulse {
  0%, 100% {
    box-shadow: 0 0 20px rgba(0, 255, 255, 0.1);
    transform: scale(1);
  }
  50% {
    box-shadow: 0 0 30px rgba(0, 255, 255, 0.2);
    transform: scale(1.05);
  }
}

.empty-icon svg {
  width: 32px;
  height: 32px;
}

.empty-text h4 {
  margin: 0 0 8px 0;
  font-size: 16px;
  font-weight: 600;
  color: rgba(0, 255, 255, 0.8);
  text-shadow: 0 0 10px rgba(0, 255, 255, 0.3);
}

.empty-text p {
  margin: 0;
  font-size: 12px;
  color: rgba(255, 255, 255, 0.5);
  line-height: 1.4;
  max-width: 200px;
}

.logs-list {
  height: 100%;
  overflow-y: auto;
  padding: 10px;
}

.logs-list::-webkit-scrollbar {
  width: 6px;
}

.logs-list::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 3px;
}

.logs-list::-webkit-scrollbar-thumb {
  background: rgba(0, 255, 255, 0.3);
  border-radius: 3px;
}

.logs-list::-webkit-scrollbar-thumb:hover {
  background: rgba(0, 255, 255, 0.5);
}

.log-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  margin-bottom: 4px;
  background:
    linear-gradient(135deg,
      rgba(255, 255, 255, 0.03) 0%,
      rgba(255, 255, 255, 0.01) 100%
    ),
    rgba(0, 0, 0, 0.25);
  border-radius: 5px;
  border: 1px solid rgba(0, 255, 255, 0.08);
  backdrop-filter: blur(3px);
  transition: all 0.2s ease;
  animation: logItemSlideIn 0.3s ease-out;
  position: relative;
}

@keyframes logItemSlideIn {
  from {
    opacity: 0;
    transform: translateX(-15px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

.log-item:hover {
  background:
    linear-gradient(135deg,
      rgba(255, 255, 255, 0.08) 0%,
      rgba(255, 255, 255, 0.03) 100%
    ),
    rgba(0, 0, 0, 0.35);
  border-color: rgba(0, 255, 255, 0.2);
  transform: translateY(-0.5px);
}

.log-item.log-info::before {
  background: linear-gradient(180deg, #00ffff, #0088ff);
}

.log-item.log-success::before {
  background: linear-gradient(180deg, #00ff88, #008844);
}

.log-item.log-warning::before {
  background: linear-gradient(180deg, #ffff00, #ff8800);
}

.log-item.log-error::before {
  background: linear-gradient(180deg, #ff4444, #cc0000);
}

.log-item::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 3px;
  border-radius: 3px 0 0 3px;
  background: #666;
}

.log-icon {
  flex-shrink: 0;
  width: 14px;
  height: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.08);
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
}

.log-item.log-info .log-icon {
  background: rgba(0, 255, 255, 0.15);
  box-shadow: 0 0 8px rgba(0, 255, 255, 0.3);
}

.log-item.log-success .log-icon {
  background: rgba(0, 255, 136, 0.15);
  box-shadow: 0 0 8px rgba(0, 255, 136, 0.3);
}

.log-item.log-warning .log-icon {
  background: rgba(255, 255, 0, 0.15);
  box-shadow: 0 0 8px rgba(255, 255, 0, 0.3);
}

.log-item.log-error .log-icon {
  background: rgba(255, 68, 68, 0.15);
  box-shadow: 0 0 8px rgba(255, 68, 68, 0.3);
}

.log-icon svg {
  width: 8px;
  height: 8px;
  opacity: 0.9;
}

.log-content {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 1px;
}

.log-message {
  font-size: 11px;
  font-weight: 400;
  color: #ffffff;
  line-height: 1.2;
  word-break: break-word;
  opacity: 0.95;
}

.log-time {
  font-size: 9px;
  color: rgba(255, 255, 255, 0.5);
  font-weight: 300;
  letter-spacing: 0.2px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .logs-header {
    padding: 12px 15px;
    flex-direction: column;
    gap: 8px;
    align-items: flex-start;
  }

  .logs-controls {
    width: 100%;
    justify-content: space-between;
  }

  .logs-stats {
    gap: 10px;
  }

  .logs-empty-state {
    padding: 20px;
  }

  .empty-icon {
    width: 48px;
    height: 48px;
    margin-bottom: 15px;
  }

  .empty-icon svg {
    width: 24px;
    height: 24px;
  }

  .empty-text h4 {
    font-size: 14px;
  }

  .empty-text p {
    font-size: 11px;
    max-width: 180px;
  }

  .log-item {
    padding: 5px 8px;
    gap: 5px;
    margin-bottom: 3px;
  }

  .log-icon {
    width: 12px;
    height: 12px;
  }

  .log-icon svg {
    width: 7px;
    height: 7px;
  }

  .log-message {
    font-size: 10px;
    line-height: 1.1;
  }

  .log-time {
    font-size: 8px;
  }
}
</style>
